using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace MyCityCab.Core
{
	/// <summary>
	/// Strongly-typed collection for the Journey class.
	/// </summary>
	[Serializable]
	public partial class JourneyCollection : ActiveList<Journey, JourneyCollection> 
	{	   
		public JourneyCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Journey table.
	/// </summary>
	[Serializable]
	public partial class Journey : ActiveRecord<Journey>
	{
		#region .ctors and Default Settings
		
		public Journey()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Journey(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Journey(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Journey(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Journey", TableType.Table, DataService.GetInstance("MyCityCab"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "Id";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = false;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarRouteId = new TableSchema.TableColumn(schema);
				colvarRouteId.ColumnName = "Route_Id";
				colvarRouteId.DataType = DbType.Int32;
				colvarRouteId.MaxLength = 0;
				colvarRouteId.AutoIncrement = false;
				colvarRouteId.IsNullable = false;
				colvarRouteId.IsPrimaryKey = false;
				colvarRouteId.IsForeignKey = true;
				colvarRouteId.IsReadOnly = false;
				colvarRouteId.DefaultSetting = @"";
				
					colvarRouteId.ForeignKeyTableName = "Route";
				schema.Columns.Add(colvarRouteId);
				
				TableSchema.TableColumn colvarFare = new TableSchema.TableColumn(schema);
				colvarFare.ColumnName = "Fare";
				colvarFare.DataType = DbType.Decimal;
				colvarFare.MaxLength = 0;
				colvarFare.AutoIncrement = false;
				colvarFare.IsNullable = false;
				colvarFare.IsPrimaryKey = false;
				colvarFare.IsForeignKey = false;
				colvarFare.IsReadOnly = false;
				colvarFare.DefaultSetting = @"";
				colvarFare.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFare);
				
				TableSchema.TableColumn colvarCurrencyCode = new TableSchema.TableColumn(schema);
				colvarCurrencyCode.ColumnName = "CurrencyCode";
				colvarCurrencyCode.DataType = DbType.AnsiStringFixedLength;
				colvarCurrencyCode.MaxLength = 3;
				colvarCurrencyCode.AutoIncrement = false;
				colvarCurrencyCode.IsNullable = false;
				colvarCurrencyCode.IsPrimaryKey = false;
				colvarCurrencyCode.IsForeignKey = true;
				colvarCurrencyCode.IsReadOnly = false;
				colvarCurrencyCode.DefaultSetting = @"";
				
					colvarCurrencyCode.ForeignKeyTableName = "Currency";
				schema.Columns.Add(colvarCurrencyCode);
				
				TableSchema.TableColumn colvarWeatherStatusId = new TableSchema.TableColumn(schema);
				colvarWeatherStatusId.ColumnName = "WeatherStatus_Id";
				colvarWeatherStatusId.DataType = DbType.Int32;
				colvarWeatherStatusId.MaxLength = 0;
				colvarWeatherStatusId.AutoIncrement = false;
				colvarWeatherStatusId.IsNullable = true;
				colvarWeatherStatusId.IsPrimaryKey = false;
				colvarWeatherStatusId.IsForeignKey = true;
				colvarWeatherStatusId.IsReadOnly = false;
				colvarWeatherStatusId.DefaultSetting = @"";
				
					colvarWeatherStatusId.ForeignKeyTableName = "WeatherStatus";
				schema.Columns.Add(colvarWeatherStatusId);
				
				TableSchema.TableColumn colvarStreetStatusId = new TableSchema.TableColumn(schema);
				colvarStreetStatusId.ColumnName = "StreetStatus_Id";
				colvarStreetStatusId.DataType = DbType.Int32;
				colvarStreetStatusId.MaxLength = 0;
				colvarStreetStatusId.AutoIncrement = false;
				colvarStreetStatusId.IsNullable = true;
				colvarStreetStatusId.IsPrimaryKey = false;
				colvarStreetStatusId.IsForeignKey = true;
				colvarStreetStatusId.IsReadOnly = false;
				colvarStreetStatusId.DefaultSetting = @"";
				
					colvarStreetStatusId.ForeignKeyTableName = "StreetStatus";
				schema.Columns.Add(colvarStreetStatusId);
				
				TableSchema.TableColumn colvarJourneyDate = new TableSchema.TableColumn(schema);
				colvarJourneyDate.ColumnName = "JourneyDate";
				colvarJourneyDate.DataType = DbType.DateTime;
				colvarJourneyDate.MaxLength = 0;
				colvarJourneyDate.AutoIncrement = false;
				colvarJourneyDate.IsNullable = false;
				colvarJourneyDate.IsPrimaryKey = false;
				colvarJourneyDate.IsForeignKey = false;
				colvarJourneyDate.IsReadOnly = false;
				
						colvarJourneyDate.DefaultSetting = @"(getdate())";
				colvarJourneyDate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarJourneyDate);
				
				TableSchema.TableColumn colvarTimeOfDayId = new TableSchema.TableColumn(schema);
				colvarTimeOfDayId.ColumnName = "TimeOfDay_Id";
				colvarTimeOfDayId.DataType = DbType.Int32;
				colvarTimeOfDayId.MaxLength = 0;
				colvarTimeOfDayId.AutoIncrement = false;
				colvarTimeOfDayId.IsNullable = true;
				colvarTimeOfDayId.IsPrimaryKey = false;
				colvarTimeOfDayId.IsForeignKey = true;
				colvarTimeOfDayId.IsReadOnly = false;
				colvarTimeOfDayId.DefaultSetting = @"";
				
					colvarTimeOfDayId.ForeignKeyTableName = "TimeOfDay";
				schema.Columns.Add(colvarTimeOfDayId);
				
				TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
				colvarUserId.ColumnName = "User_Id";
				colvarUserId.DataType = DbType.Int32;
				colvarUserId.MaxLength = 0;
				colvarUserId.AutoIncrement = false;
				colvarUserId.IsNullable = true;
				colvarUserId.IsPrimaryKey = false;
				colvarUserId.IsForeignKey = true;
				colvarUserId.IsReadOnly = false;
				colvarUserId.DefaultSetting = @"";
				
					colvarUserId.ForeignKeyTableName = "User";
				schema.Columns.Add(colvarUserId);
				
				TableSchema.TableColumn colvarCarModelId = new TableSchema.TableColumn(schema);
				colvarCarModelId.ColumnName = "CarModel_Id";
				colvarCarModelId.DataType = DbType.Int32;
				colvarCarModelId.MaxLength = 0;
				colvarCarModelId.AutoIncrement = false;
				colvarCarModelId.IsNullable = true;
				colvarCarModelId.IsPrimaryKey = false;
				colvarCarModelId.IsForeignKey = true;
				colvarCarModelId.IsReadOnly = false;
				colvarCarModelId.DefaultSetting = @"";
				
					colvarCarModelId.ForeignKeyTableName = "CarModel";
				schema.Columns.Add(colvarCarModelId);
				
				TableSchema.TableColumn colvarRate = new TableSchema.TableColumn(schema);
				colvarRate.ColumnName = "Rate";
				colvarRate.DataType = DbType.Decimal;
				colvarRate.MaxLength = 0;
				colvarRate.AutoIncrement = false;
				colvarRate.IsNullable = false;
				colvarRate.IsPrimaryKey = false;
				colvarRate.IsForeignKey = false;
				colvarRate.IsReadOnly = false;
				
						colvarRate.DefaultSetting = @"((0))";
				colvarRate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRate);
				
				TableSchema.TableColumn colvarStory = new TableSchema.TableColumn(schema);
				colvarStory.ColumnName = "Story";
				colvarStory.DataType = DbType.String;
				colvarStory.MaxLength = 1073741823;
				colvarStory.AutoIncrement = false;
				colvarStory.IsNullable = true;
				colvarStory.IsPrimaryKey = false;
				colvarStory.IsForeignKey = false;
				colvarStory.IsReadOnly = false;
				colvarStory.DefaultSetting = @"";
				colvarStory.ForeignKeyTableName = "";
				schema.Columns.Add(colvarStory);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MyCityCab"].AddSchema("Journey",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("Id")]
		public int Id 
		{
			get { return GetColumnValue<int>("Id"); }

			set { SetColumnValue("Id", value); }

		}

		  
		[XmlAttribute("RouteId")]
		public int RouteId 
		{
			get { return GetColumnValue<int>("Route_Id"); }

			set { SetColumnValue("Route_Id", value); }

		}

		  
		[XmlAttribute("Fare")]
		public decimal Fare 
		{
			get { return GetColumnValue<decimal>("Fare"); }

			set { SetColumnValue("Fare", value); }

		}

		  
		[XmlAttribute("CurrencyCode")]
		public string CurrencyCode 
		{
			get { return GetColumnValue<string>("CurrencyCode"); }

			set { SetColumnValue("CurrencyCode", value); }

		}

		  
		[XmlAttribute("WeatherStatusId")]
		public int? WeatherStatusId 
		{
			get { return GetColumnValue<int?>("WeatherStatus_Id"); }

			set { SetColumnValue("WeatherStatus_Id", value); }

		}

		  
		[XmlAttribute("StreetStatusId")]
		public int? StreetStatusId 
		{
			get { return GetColumnValue<int?>("StreetStatus_Id"); }

			set { SetColumnValue("StreetStatus_Id", value); }

		}

		  
		[XmlAttribute("JourneyDate")]
		public DateTime JourneyDate 
		{
			get { return GetColumnValue<DateTime>("JourneyDate"); }

			set { SetColumnValue("JourneyDate", value); }

		}

		  
		[XmlAttribute("TimeOfDayId")]
		public int? TimeOfDayId 
		{
			get { return GetColumnValue<int?>("TimeOfDay_Id"); }

			set { SetColumnValue("TimeOfDay_Id", value); }

		}

		  
		[XmlAttribute("UserId")]
		public int? UserId 
		{
			get { return GetColumnValue<int?>("User_Id"); }

			set { SetColumnValue("User_Id", value); }

		}

		  
		[XmlAttribute("CarModelId")]
		public int? CarModelId 
		{
			get { return GetColumnValue<int?>("CarModel_Id"); }

			set { SetColumnValue("CarModel_Id", value); }

		}

		  
		[XmlAttribute("Rate")]
		public decimal Rate 
		{
			get { return GetColumnValue<decimal>("Rate"); }

			set { SetColumnValue("Rate", value); }

		}

		  
		[XmlAttribute("Story")]
		public string Story 
		{
			get { return GetColumnValue<string>("Story"); }

			set { SetColumnValue("Story", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public MyCityCab.Core.JourneyCommentCollection JourneyCommentRecords()
		{
			return new MyCityCab.Core.JourneyCommentCollection().Where(JourneyComment.Columns.JourneyId, Id).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a CarModel ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.CarModel CarModel
		{
			get { return MyCityCab.Core.CarModel.FetchByID(this.CarModelId); }

			set { SetColumnValue("CarModel_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a Currency ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.Currency Currency
		{
			get { return MyCityCab.Core.Currency.FetchByID(this.CurrencyCode); }

			set { SetColumnValue("CurrencyCode", value.CurrencyCode); }

		}

		
		
		/// <summary>
		/// Returns a Route ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.Route Route
		{
			get { return MyCityCab.Core.Route.FetchByID(this.RouteId); }

			set { SetColumnValue("Route_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a StreetStatus ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.StreetStatus StreetStatus
		{
			get { return MyCityCab.Core.StreetStatus.FetchByID(this.StreetStatusId); }

			set { SetColumnValue("StreetStatus_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a TimeOfDay ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.TimeOfDay TimeOfDay
		{
			get { return MyCityCab.Core.TimeOfDay.FetchByID(this.TimeOfDayId); }

			set { SetColumnValue("TimeOfDay_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a User ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.User User
		{
			get { return MyCityCab.Core.User.FetchByID(this.UserId); }

			set { SetColumnValue("User_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a WeatherStatus ActiveRecord object related to this Journey
		/// 
		/// </summary>
		public MyCityCab.Core.WeatherStatus WeatherStatus
		{
			get { return MyCityCab.Core.WeatherStatus.FetchByID(this.WeatherStatusId); }

			set { SetColumnValue("WeatherStatus_Id", value.Id); }

		}

		
		
		#endregion
		
		
		
		#region Many To Many Helpers
		
		 
		public MyCityCab.Core.CommentCollection GetCommentCollection() { return Journey.GetCommentCollection(this.Id); }

		public static MyCityCab.Core.CommentCollection GetCommentCollection(int varId)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM Comment INNER JOIN JourneyComment ON "+
				"Comment.Id=JourneyComment.Comment_Id WHERE JourneyComment.Journey_Id=@Journey_Id", Journey.Schema.Provider.Name);
			
			cmd.AddParameter("@Journey_Id", varId, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			CommentCollection coll = new CommentCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveCommentMap(int varId, CommentCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Journey_Id=@Journey_Id", Journey.Schema.Provider.Name);
			cmdDel.AddParameter("@Journey_Id", varId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Comment item in items)
			{
				JourneyComment varJourneyComment = new JourneyComment();
				varJourneyComment.SetColumnValue("Journey_Id", varId);
				varJourneyComment.SetColumnValue("Comment_Id", item.GetPrimaryKeyValue());
				varJourneyComment.Save();
			}

		}

		public static void SaveCommentMap(int varId, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Journey_Id=@Journey_Id", Journey.Schema.Provider.Name);
			cmdDel.AddParameter("@Journey_Id", varId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					JourneyComment varJourneyComment = new JourneyComment();
					varJourneyComment.SetColumnValue("Journey_Id", varId);
					varJourneyComment.SetColumnValue("Comment_Id", l.Value);
					varJourneyComment.Save();
				}

			}

		}

		public static void SaveCommentMap(int varId , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Journey_Id=@Journey_Id", Journey.Schema.Provider.Name);
			cmdDel.AddParameter("@Journey_Id", varId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				JourneyComment varJourneyComment = new JourneyComment();
				varJourneyComment.SetColumnValue("Journey_Id", varId);
				varJourneyComment.SetColumnValue("Comment_Id", item);
				varJourneyComment.Save();
			}

		}

		
		public static void DeleteCommentMap(int varId) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Journey_Id=@Journey_Id", Journey.Schema.Provider.Name);
			cmdDel.AddParameter("@Journey_Id", varId);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varId,int varRouteId,decimal varFare,string varCurrencyCode,int? varWeatherStatusId,int? varStreetStatusId,DateTime varJourneyDate,int? varTimeOfDayId,int? varUserId,int? varCarModelId,decimal varRate,string varStory)
		{
			Journey item = new Journey();
			
			item.Id = varId;
			
			item.RouteId = varRouteId;
			
			item.Fare = varFare;
			
			item.CurrencyCode = varCurrencyCode;
			
			item.WeatherStatusId = varWeatherStatusId;
			
			item.StreetStatusId = varStreetStatusId;
			
			item.JourneyDate = varJourneyDate;
			
			item.TimeOfDayId = varTimeOfDayId;
			
			item.UserId = varUserId;
			
			item.CarModelId = varCarModelId;
			
			item.Rate = varRate;
			
			item.Story = varStory;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,int varRouteId,decimal varFare,string varCurrencyCode,int? varWeatherStatusId,int? varStreetStatusId,DateTime varJourneyDate,int? varTimeOfDayId,int? varUserId,int? varCarModelId,decimal varRate,string varStory)
		{
			Journey item = new Journey();
			
				item.Id = varId;
				
				item.RouteId = varRouteId;
				
				item.Fare = varFare;
				
				item.CurrencyCode = varCurrencyCode;
				
				item.WeatherStatusId = varWeatherStatusId;
				
				item.StreetStatusId = varStreetStatusId;
				
				item.JourneyDate = varJourneyDate;
				
				item.TimeOfDayId = varTimeOfDayId;
				
				item.UserId = varUserId;
				
				item.CarModelId = varCarModelId;
				
				item.Rate = varRate;
				
				item.Story = varStory;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"Id";
			 public static string RouteId = @"Route_Id";
			 public static string Fare = @"Fare";
			 public static string CurrencyCode = @"CurrencyCode";
			 public static string WeatherStatusId = @"WeatherStatus_Id";
			 public static string StreetStatusId = @"StreetStatus_Id";
			 public static string JourneyDate = @"JourneyDate";
			 public static string TimeOfDayId = @"TimeOfDay_Id";
			 public static string UserId = @"User_Id";
			 public static string CarModelId = @"CarModel_Id";
			 public static string Rate = @"Rate";
			 public static string Story = @"Story";
						
		}

		#endregion
	}

}

